<%@ page language="java" contentType="text/json; charset=UTF-8"
    pageEncoding="UTF-8" import="java.util.*, java.net.*, java.sql.*, java.io.*, org.json.simple.*"%>
    
<%!

/**
 * This JSP returns additional info needed by openGeoPortal to display layers from HGL.
 * Given a list of layers, it returns the specified parameters from HGL's layer database.
 * Results are returned in JSON format. 
 *
 * Ideally, we would also return the geoserver workspace for the layer.  It could be done with wfs & wcs describe
 * layer calls to geoserver, but we would also have to pass whether the layer was a vector or raster.
 */
 public static Map<String, Map<String, String>> getLayerInfoFromGS_NEW(String[] layerNames) throws IOException {
		Map<String, Map<String, String>> fullResultSet = new HashMap<String, Map<String, String>>();
	    java.sql.ResultSet _resultSet = null;
	    java.sql.Connection _con = null;

	    String whereClause = "";
	    int i;
		for (i = 0; i < (layerNames.length - 1); i++){
			//some really basic string processing to prevent SQL injection problems
			String currentLayerName = layerNames[i].replace(";", "").replace("'", "").replace("\"", "").replace("\\", "");
			whereClause += "GS_NEW.TABLE_NAME = '" + currentLayerName + "' OR ";
		}
		whereClause += "GS_NEW.TABLE_NAME = " + layerNames[layerNames.length - 1];
		//do I need anything else?
	    String theQuery = "SELECT GS_NEW.TABLE_NAME, GS_NEW.ACCESS_FLAG, ALL_TABLES.OWNER FROM GS_NEW "
	    	+ "INNER JOIN ALL_TABLES ON GS_NEW.TABLE_NAME = ALL_TABLES.TABLE_NAME WHERE " + whereClause;
	    String jdbcConnect = "jdbc:oracle:thin:@alcott.hul.harvard.edu:8403:hglprd";
	    String user = "sde";
	    String pass = "xxxxx";
	    String driver = "oracle.jdbc.OracleDriver";

	        try {
	            Class.forName(driver).newInstance();
	            _con = java.sql.DriverManager.getConnection(jdbcConnect, user, pass);

	            Statement stmnt = _con.createStatement();
	            ResultSet rs = stmnt.executeQuery(theQuery);
	            while (rs.next()) {
	            	Map<String, String> rowResultSet = new HashMap<String, String>();
	            	rowResultSet.put("access", rs.getString("GS_NEW.ACCESS_FLAG"));
	            	rowResultSet.put("owner", rs.getString("ALL_TABLES.OWNER"));
					fullResultSet.put(rs.getString("GS_NEW.TABLE_NAME"), rowResultSet);
	            }

	            rs.close();
	            stmnt.close();
	            _con.close();

	        } catch (InstantiationException e) {
	            e.printStackTrace();
	        } catch (IllegalAccessException e) {
	            e.printStackTrace();
	        } catch (ClassNotFoundException e) {
	            e.printStackTrace();
	        } catch (SQLException e) {
	            e.printStackTrace();
	        }
		    return fullResultSet;
	  }
%>


<%
	if (request.getMethod() != "POST"){
		response.setStatus(403);
	}
    //ArcSDE layer name (= table name)
	String[] layers = request.getParameterValues("layers");
	String validationKey = request.getParameter("ValidationKey");
	String callback = request.getParameter("callback");
	
	if (layers.length < 1){
		throw new Exception("No layers specified in query.");
	}
	if (!validationKey.equals("OPENGEOPORTALROCKS")){
		response.setStatus(403);
	}
 	 	
	try {
		Map<String, Map<String, String>> results = getLayerInfoFromGS_NEW(layers);
		if (results.isEmpty()){
			throw new Exception("No results returned from GS_NEW");
		}
		//now convert results to JSON w/ simple JSON library
		String jsonString = JSONValue.toJSONString(results);
		if (callback == null){
			out.write(jsonString);
		} else {
			//if a callback is supplied, output jsonp
			String jsonpString = callback + "(" + jsonString + ");";
			out.write(jsonpString);
		}
	} catch (Exception e){
    	response.setStatus(500);
	}
%>
